package com.jsjosem.aftersales.ui.login;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;

import com.jsjosem.aftersales.R;
import com.jsjosem.aftersales.databinding.ActivityForgetPasswordBinding;
import com.jsjosem.aftersales.request.VerifyCodeRequest;
import com.jsjosem.aftersales.utils.ImmUtils;

import java.util.Objects;

import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;

public class ForgotPasswordActivity extends AppCompatActivity {

    private final VerifyCodeRequest verifyCodeRequest = new VerifyCodeRequest();

    private String phone;

    private String password;

    private CountDownTimer start;

    private ActivityForgetPasswordBinding binding;
    private EditText forgotPasswordPhoneEt, forgotPasswordVerifyCodeEt, forgotPasswordChangePasswordEt, forgotPasswordPasswordConfirmEt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityForgetPasswordBinding.inflate(getLayoutInflater());
        ConstraintLayout view = binding.getRoot();
        setContentView(view);
        initView();
    }

    private void initView() {
        forgotPasswordPhoneEt = findViewById(R.id.forgot_password_phone);
        forgotPasswordVerifyCodeEt = findViewById(R.id.forgot_password_verify_code);
        forgotPasswordChangePasswordEt = findViewById(R.id.forgot_password_change_password);
        forgotPasswordPasswordConfirmEt = findViewById(R.id.forgot_password_password_confirm);
        ImmUtils.setEditTextHintFontSize(forgotPasswordPhoneEt, 12, "请输入手机号");
        ImmUtils.setEditTextHintFontSize(forgotPasswordVerifyCodeEt, 12, "请输入验证码");
        ImmUtils.setEditTextHintFontSize(forgotPasswordChangePasswordEt, 12, "请输入密码");
        ImmUtils.setEditTextHintFontSize(forgotPasswordPasswordConfirmEt, 12, "请确认密码");

        // 点击获取验证码
        clickGetVerifyCode();

        // 点击确定修改
        clickConfirmBt();

    }

    private void clickGetVerifyCode() {
        TextView getVerifyCodeTv = findViewById(R.id.change_password_get_verify_code);
        getVerifyCodeTv.setOnClickListener(view -> {
            if (Objects.nonNull(start)) {
                return;
            }
            if (validatePhone()) {
                return;
            }
            // 发起发送验证码请求
//            verifyCodeRequest.send(phone, (res) -> {
            // 发送成功，修改获取验证码的TextView，并且变成60秒倒计时
            start = new CountDownTimer(60000, 1000) {
                public void onTick(long millisUntilFinished) {
                    getVerifyCodeTv.setText(millisUntilFinished / 1000 + "秒后可重新获取");
                    getVerifyCodeTv.setTextColor(ContextCompat.getColor(ForgotPasswordActivity.this, R.color.text_check_box_color));
                }

                public void onFinish() {
                    getVerifyCodeTv.setText(R.string.get_verify_code_text);
                    getVerifyCodeTv.setTextColor(ContextCompat.getColor(ForgotPasswordActivity.this, R.color.text_view_color));
                }
            }.start();
//            }, failedMessage -> Toast.makeText(ForgotPasswordActivity.this, failedMessage.toString(), Toast.LENGTH_SHORT).show());
        });
    }

    private void clickConfirmBt() {
        TextView immediatelyChangePasswordBt = findViewById(R.id.immediately_change_password_bt);
        immediatelyChangePasswordBt.setOnClickListener(view -> {
            // 表单字段校验
            if (validateForm()) {
                return;
            }

            // TODO 修改密码
            Toast.makeText(ForgotPasswordActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
            // 修改成功，返回登录页
            finish();
        });
    }

    private boolean validateForm() {
        if (validatePhone()) {
            return true;
        }

        // 校验验证码是否输入
        EditText verifyCodeEt = findViewById(R.id.forgot_password_verify_code);
        Editable verifyCodeText = verifyCodeEt.getText();
        if (verifyCodeText.length() != 5) {
            verifyCodeEt.setError("请输入正确的验证码");
            return true;
        }

        EditText changePasswordEt = findViewById(R.id.forgot_password_change_password);
        Editable changePasswordText = changePasswordEt.getText();
        if (changePasswordText.length() < 6 || changePasswordText.length() > 20) {
            changePasswordEt.setError("请设置6~20位的密码");
            return true;
        }

        EditText changePasswordConfirmEt = findViewById(R.id.forgot_password_password_confirm);
        Editable changePasswordConfirmText = changePasswordConfirmEt.getText();
        if (changePasswordConfirmText.length() < 6 || changePasswordConfirmText.length() > 20) {
            changePasswordConfirmEt.setError("请设置6~20位的密码");
            return true;
        }

        String changePasswordConfirmContent = changePasswordConfirmText.toString();
        String changePasswordContent = changePasswordText.toString();
        if (!StrUtil.equals(changePasswordContent, changePasswordConfirmContent)) {
            changePasswordConfirmEt.setError("两次输入的密码不一致");
            return true;
        }
        password = changePasswordConfirmContent;
        return false;
    }

    private boolean validatePhone() {
        EditText phoneEt = findViewById(R.id.forgot_password_phone);
        if (phoneEt.getText().length() <= 0) {
            phoneEt.setError("请输入手机号");
            return true;
        }
        phone = phoneEt.getText().toString();
        if (!PhoneUtil.isMobile(phone)) {
            phoneEt.setError("请输入正确的手机号");
            return true;
        }
        return false;
    }
}